/**
 *
 * @author Tin, Tien, Cuong
 */
package uit.tkorg.coauthorgraph.utility;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import uit.tkorg.coauthorgraph.dto.PaperDTO;
import uit.tkorg.coauthorgraph.property.file.MASearchCrawlPatterns;

public class MASResultAnalyze {  
    /**
    * get the list of publication for the specified author
    * @param htmlContent: the content of returned HTML page
    * @param authorID
    * @return: the list of PaperDTO
    */
    public static ArrayList getPublications(String htmlContent, String authorName) {
    	ArrayList publicationList = new ArrayList();
    	CommonHTMLProcessor htmlParser = new CommonHTMLProcessor();
        int numPublication = getNumberPublications(htmlContent);
        try {
            if(numPublication != 0) {
                int start = 1;
                int end = MASearchCrawlPatterns.MAX_NUMBER_SHOW_IN_PAGE;
                int numPubPerPage = MASearchCrawlPatterns.MAX_NUMBER_SHOW_IN_PAGE;
                int numPage = (numPublication)/numPubPerPage;
                for (int i = 0; i <= numPage; i++) {
                    int MaxRunPerPage = numPubPerPage;
                    if (i == numPage) {
                        MaxRunPerPage = numPublication - numPubPerPage * numPage;
                    }

                    authorName = authorName.replaceAll(" ", "+");
                    String htmlContentOfPublications = CommonURLQuery.getHTMLPageContentWithUTF8 (new URL(
                                            MASearchCrawlPatterns.ACCADEMIC_PUBLICATION_QUERY + authorName +
                                            MASearchCrawlPatterns.AND +
                                            MASearchCrawlPatterns.START + "=" + start +
                                            MASearchCrawlPatterns.AND +
                                            MASearchCrawlPatterns.END + "=" + end));

                    for (int j = 0; j < MaxRunPerPage; j++) {
                        String paperID = String.valueOf(j);
                        if (j < 10) {
                            paperID = "0" + paperID;
                        }

                        PaperDTO paperDTO = new PaperDTO();

                        String paperItemContent = getTextContentOfSpecifiedPublication(htmlContentOfPublications, paperID);
                        // Title
                        String title = getTitleOfPublication(paperItemContent, paperID);
                        title = title.replaceAll("(\r\n|\r|\n|\n\r)", "");
                        paperDTO.setTitle(title);

                        // List Author
                        ArrayList authorNameList = getListAuthorNameOfPublication(paperItemContent);
                        paperDTO.setAuthorNameList(authorNameList);

                        // Conferece - Journal
                        String conference = getNameConferenceOfPublication(paperItemContent, paperID);
                        if(conference != null && !conference.equalsIgnoreCase(""))
                            paperDTO.setConferenceName(conference);
                        else {
                            String journalName = getNameJournalOfPublication(paperItemContent, paperID);
                            paperDTO.setJournalName(journalName);
                        }

                        // Page number
                        String pageNumber = getPageNumberOfPublication(paperItemContent, paperID);
                        paperDTO.setPages(pageNumber);

                        // Year publication
                        int year = getYearPublishOfPublication(paperItemContent, paperID);
                        paperDTO.setYear(year);

                        publicationList.add(paperDTO);
                    }

                    start = start + numPubPerPage;
                    end = end + numPubPerPage;
                }
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        
    	return publicationList;
    }
    
    /**
    *
    * @param coAuthorArea :
    * @return
    * ID = ctl00_LeftPanel_CoAuthors_PanelHeader
    */
    public static int getAuthorID (String coAuthorArea){
       int id = 0;
       int start = coAuthorArea.indexOf(";id=", -1);
       int end = coAuthorArea.indexOf("\"",start);
       String interest = coAuthorArea.substring(start+4,end);
       id = Integer.parseInt(interest);
       return id;

    }

    /**
    * Lay linh vuc nghien cuu cua tac gia
    * @param htmlContentAsText : noi dung cua trang duoi dang Text
    * @return
    */
    public static String getInterest (String htmlContentAsText) {
        int start = htmlContentAsText.indexOf("Interest:", -1);
        int end = htmlContentAsText.indexOf("Collaborated",start);
        String interest = htmlContentAsText.substring(start+10,end);
        interest.trim();
        return interest;
    }

   /**
    *
    * @param authorTextArea
    * @return
    *
    * ID : ctl00_MainContent_AuthorItem_authorName
    */
   public static String getAuthorName(String authorTextArea) {
       String authorName;
       authorName = authorTextArea.toString().replaceAll("\\<.*?>","");
       return authorName;
   }

   /**
    *
    * @param authorTextArea :noi dung text sau khi da lay id trong dvi
    * @return
    *  id : ctl00_MainContent_AuthorItem_publication
    */
   public static int getNumberPublications(String htmlContent) {
       int numberOfPublication;
       String publNumArea = CommonHTMLProcessor.getHTMLContentOfDivTag(htmlContent, MASearchCrawlPatterns.PATTERN_PUBLICATION_COUNT);
       publNumArea = publNumArea.toString().replaceAll("\\<.*?>","");
       publNumArea = getPattern(publNumArea,"\\d+",0);
       numberOfPublication=Integer.parseInt(publNumArea);
       return numberOfPublication;
   }

   /**
    *
    * @param authorTextArea
    * @return
    *  id: ctl00_LeftPanel_CoAuthors_PanelHeader
    */
   public static int getNumberCoAuthor(String coAuthorTextArea) {
       int numberPublication = 0;
       coAuthorTextArea = coAuthorTextArea.toString().replaceAll("\\<.*?>","");
       coAuthorTextArea = getPattern(coAuthorTextArea,"\\d+",0);
       numberPublication = Integer.parseInt(coAuthorTextArea);
       return numberPublication;
   }

   /**
    *
    * @param Journal
    * @return
    * ID: ctl00_LeftPanel_RelatedJournals_PanelHeader
    */
   public static int getNumberJournal(String journalTextArea) {
       int numberJournal = 0;
       journalTextArea = journalTextArea.toString().replaceAll("\\<.*?>","");
       journalTextArea = getPattern(journalTextArea,"\\d+",0);
       numberJournal = Integer.parseInt(journalTextArea);
       return numberJournal;
   }

   /**
    *
    * @param journalTextArea
    * @return
    * ID : ctl00_LeftPanel_RelatedConferences_PanelHeader
    */
   public static int getNumberConference(String conferemceTextArea) {
       int numberConference = 0;
       conferemceTextArea = conferemceTextArea.toString().replaceAll("\\<.*?>","");
       conferemceTextArea = getPattern(conferemceTextArea,"\\d+",0);
       numberConference=Integer.parseInt(conferemceTextArea);
       return numberConference;
   }

   /**
    *
    * @param keywordTextArea
    * @return
    * ID : ctl00_LeftPanel_RelatedKeywords_PanelHeade
    */
   public static int getNumberKeyword(String keywordTextArea) {
       int numberJournal = 0;
       keywordTextArea = keywordTextArea.toString().replaceAll("\\<.*?>","");
       keywordTextArea = getPattern(keywordTextArea,"\\d+",0);
       numberJournal=Integer.parseInt(keywordTextArea);
       return numberJournal;
   }
   
   /**
   *
   * @param htmlContent : string to find
   * @param _patternStr : pattern to match
   * @param position    : position to get
   * @return
   */
   public static String getPattern(String htmlContent,String _patternStr, int position){
      String result;
      Pattern _pattern = Pattern.compile(_patternStr);
      Matcher _matcher =  _pattern.matcher(htmlContent);
      _matcher.find();
      result = _matcher.group(position).toString();
      return result;
   }
   
   /**
     * get the TextContent of the specified publication
     * @param pageContent : HTML Content of publication page
     * @param pubOrder : position of Pub
     * @return : String Text Info Text.
     */
    public static String getTextContentOfSpecifiedPublication(String pageContent, String pubOrder) {
        String ptGetPaperItem = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST
                                                        + pubOrder + MASearchCrawlPatterns.PATTERN_DIV_TITLE;
        String result = "";
        int _startIndex = 0;
        int _endIndex = 0;
        if(pageContent != null && pageContent != ""){
            _startIndex = pageContent.indexOf(ptGetPaperItem);
            _endIndex = pageContent.indexOf(MASearchCrawlPatterns.LI_CLOSE_TAB, _startIndex);
        }
        if(_startIndex != -1 && _endIndex != -1)
            result = pageContent.substring(_startIndex, _endIndex);

        return result;
    }

    /**
     * Ham lay danh sach ten cac tac gia cua bai bao.
     * @param publicationTextContent
     * @return : ArrayList Author Name
     */
    public static ArrayList getListAuthorNameOfPublication(String publicationTextContent){
        ArrayList lstAuthor = new ArrayList();
        int _startIndex = 0;
        int _endIndex = 0;

        if (publicationTextContent != null && publicationTextContent != ""){
            _startIndex = publicationTextContent.indexOf(MASearchCrawlPatterns.PATTERN_DIV_CLASS_LIST_AUTHOR_NAME);
            _endIndex = publicationTextContent.indexOf(MASearchCrawlPatterns.DIV_CLOSE_TAB, _startIndex);
        }
        if (_startIndex != -1 && _endIndex != -1){
            String listAuthorName = publicationTextContent.substring(_startIndex, _endIndex);
            _startIndex = listAuthorName.indexOf(MASearchCrawlPatterns.PATTERN_HREF_CLASS_LIST_AUTHOR_NAME);
            _endIndex = listAuthorName.lastIndexOf(MASearchCrawlPatterns.HREF_CLOSE);
            if (_startIndex != -1 && _endIndex != -1) {
                listAuthorName = listAuthorName.substring(_startIndex, _endIndex);
                listAuthorName = listAuthorName.replaceAll(MASearchCrawlPatterns.HTML_TAB,"").replaceAll(MASearchCrawlPatterns.HTML_TAB_OTHER, "");
                lstAuthor = getAuthorNameFromTextListAuthorName(listAuthorName, ",");
            }
        }

        return lstAuthor;
    }

    /**
     * Lay ten tac gia tu chuoi danh sach ten tac gia
     * @param listAuthorName
     * @param characterBetwenAuthorName
     * @return
     */
    public static ArrayList getAuthorNameFromTextListAuthorName(String listAuthorName, String characterBetwenAuthorName) {
        int beginIndex = 0;
        int temp = -1;
        String authorName = "";
        ArrayList strList = new ArrayList();
        while(beginIndex != -1){
            temp =  beginIndex;
            beginIndex = listAuthorName.indexOf(characterBetwenAuthorName , beginIndex + 1);
            if(beginIndex == -1) {
                authorName = listAuthorName.substring(temp).replaceAll("  ", "").replaceAll(",", "");
                strList.add(authorName.trim());
            }
            else {
                authorName = listAuthorName.substring(temp, beginIndex).replaceAll("  ", "").replaceAll(",", "");
                strList.add(authorName.trim());
            }

        }

        return strList;
    }
	
    /**
     * Ham lay tom tat cua tac gia tu noi dung cua bai bao.
     * @param publicationTextContent
     * @return
     */
    public static String getAbstractOfPublication(String publicationTextContent){
        String result = "";
        int _beginIndex = 0;
        int _endIndex = 0;
        if(publicationTextContent != null && publicationTextContent != ""){
            _beginIndex = publicationTextContent.indexOf(MASearchCrawlPatterns.DIV_CLASS_ABSTRACT_CONTENT);
            _endIndex = publicationTextContent.indexOf(MASearchCrawlPatterns.DIV_CLOSE_TAB, _beginIndex);

        }
        if(_beginIndex != -1 && _endIndex != -1){
            result = publicationTextContent.substring(_beginIndex, _endIndex);
        }
        result = result.replaceAll(MASearchCrawlPatterns.HTML_TAB, "").replaceAll(MASearchCrawlPatterns.HTML_TAB_OTHER, "").replaceAll( "  ", "");

        return result;
    }
	
    public static String getTitleOfPublication(String publicationTextContent, String pubOrder){
        String result = "";
        result = CommonHTMLProcessor.getTextOfDivTag(publicationTextContent,
                        MASearchCrawlPatterns.PUBLICATION_PATTERN_DIV.replaceAll(
                                        "\\(NUM\\)", pubOrder));
        return result;
    }
	
    /**
     * get the name of Conference or Journal
     * @param publicationTextContent
     * @param pubOrder : the order number of the specified pub in page
     * @return
     */
    public static String getNameConferenceOfPublication(String publicationTextContent, String pubOrder){
        String idOfConferenceTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.HL_CONFERENCE;
        String result = "";
        if(CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfConferenceTab) != null)
            result = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfConferenceTab).replaceAll(MASearchCrawlPatterns.HTML_TAB,"");
        return result;
    }
    /**
     * Ham lay ten Joural cua publication neu conference Rong
     * @param publicationTextContent
     * @param pubOrder
     * @return
     */
    public static String getNameJournalOfPublication(String publicationTextContent, String pubOrder){
        String result = "";
        String idOfJouralTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.HL_JOURNAL;
            if(CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfJouralTab) != null)
                result = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfJouralTab).replaceAll(MASearchCrawlPatterns.HTML_TAB,"");
        return result;
    }
	
    /**
     * get the page number of publications
     * @param publicationTextContent
     * @param pubOrder
     * @return
     */
    public static String getPageNumberOfPublication(String publicationTextContent, String pubOrder){
        String result = "";
        String idOfYearTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.YEAR_CONFERENCE;
        String strContainPageNumber = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfYearTab);
        if (strContainPageNumber == null || strContainPageNumber.equalsIgnoreCase("")){
            idOfYearTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.YEAR_JOURNAL;
            strContainPageNumber = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfYearTab);
            if (strContainPageNumber == null || strContainPageNumber.equalsIgnoreCase("")) {
                idOfYearTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.YEAR_PUBLISHED;
                strContainPageNumber = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfYearTab);
            }
        }

        if (strContainPageNumber != null)
            result = strContainPageNumber.replaceAll(MASearchCrawlPatterns.HTML_TAB, "");

        Pattern hitsPattern = Pattern.compile(MASearchCrawlPatterns.PATTERN_PAGENUM_TO_PAGENUM);
        Matcher m = hitsPattern.matcher(result);
        if (!m.find()) {
            return "";
        }
        else {
            String firstNumber = m.group(1);
            String lastNumber = m.group(2);

            firstNumber = firstNumber.replaceAll(",", "");
            lastNumber = lastNumber.replaceAll(",", "");
            result = firstNumber + " - " + lastNumber;
        }

        return result;
    }
	
    /**
     * Ham lay nam cong bo
     * @param publicationTextContent
     * @param pubOrder
     * @return
     */

    public static int getYearPublishOfPublication(String publicationTextContent, String pubOrder){
        String result = "";
        String idOfYearTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.YEAR_CONFERENCE;
        String strContainPageNumber = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfYearTab);
        if (strContainPageNumber == null || strContainPageNumber.equalsIgnoreCase("")){
            idOfYearTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.YEAR_JOURNAL;
            strContainPageNumber = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfYearTab);
            if (strContainPageNumber == null || strContainPageNumber.equalsIgnoreCase("")) {
                idOfYearTab = MASearchCrawlPatterns.PATTERN_PUBLICATION_ITEM_IN_LIST + pubOrder + MASearchCrawlPatterns.YEAR_PUBLISHED;
                strContainPageNumber = CommonHTMLProcessor.getHTMLContentOfDivTag(publicationTextContent, idOfYearTab);
            }
        }

        if (strContainPageNumber != null)
            result = strContainPageNumber.replaceAll(MASearchCrawlPatterns.HTML_TAB, "");

        int year = 0;
        Pattern datePattern = Pattern.compile(MASearchCrawlPatterns.PATTERN_YEAR);
        Matcher m = datePattern.matcher(result);

        while (m.find()) {
            year = Integer.parseInt(m.group(m.groupCount()));
        }

        if (year>1900 && year<2100)
            return year;
        else return 0;
    }
}
